import { reactive } from 'vue';
import { V_USER } from '@/api/url/erp/baseMessage';
import { getPurtcd, getArrivalInspect, arrivalInspectBatch, getjhd } from '@/api/url/erp/logisticDocuments';
import { sliceTime } from '@/utils/time';
export default function importConfig() {
  const base = reactive<any>({
    formData: {
      MF001: '', // 用户编号
      MF002: '', // 用户名称
      MF007: '', // 部门编号
      MODI_DATE: '', // 最后修改时间
    },
    columns: [
      {
        title: '单别',
        dataIndex: 'TA001',
        checked: true,
      },
      {
        title: '单号',
        dataIndex: 'TA002',
        checked: true,
      },
      {
        title: '序号',
        dataIndex: 'TA003',
        checked: true,
      },
      {
        title: '检验批次',
        dataIndex: 'TAC04',
        checked: true,
      },
      {
        title: '审核码',
        dataIndex: 'TA014',
        checked: true,
      },
      {
        title: '验收数量',
        dataIndex: 'TAC03',
        checked: true,
      },
      {
        title: '检测方式',
        dataIndex: 'TA012',
        checked: true,
      },
      {
        title: '检测人员',
        dataIndex: 'TA006',
        checked: true,
      },
      {
        title: '审核人',
        dataIndex: 'TA020',
        checked: true,
      },
      {
        title: '检测数量',
        dataIndex: 'TA022',
        checked: true,
      },
      {
        title: '不合格数',
        dataIndex: 'TA023',
        checked: true,
      },
      {
        title: '验退数',
        dataIndex: 'TA008',
        checked: true,
      },
      {
        title: '检测结果',
        dataIndex: 'TA011',
        checked: true,
      },
      {
        title: '到货时间',
        dataIndex: 'TA004',
        checked: true,
      },
      {
        title: '检测时间',
        dataIndex: 'TA005',
        checked: true,
      },
      {
        title: '备注',
        dataIndex: 'TA015',
        checked: true,
      },
      {
        title: '操作',
        slotName: 'optional',
        fixed: 'right',
        width: 150,
        checked: true,
      },
    ],
    data: [],
    rules: {
      MF001: [
        {
          required: true,
          message: '请输入用户编号',
        },
      ],
      MF002: [
        {
          required: true,
          message: '请输入用户名称',
        },
      ],
      MF007: [
        {
          required: true,
          message: '请输入部门编号',
        },
      ],
    },
    config: ['add'],
    searchBar: '隐藏搜索',
    
    ModelTitle: '新增用户',
    visible: false, //遮罩层显示
    delVisible: false, //删除用户遮罩层
    delUserMsg: {},
    handleSubmit: null,
    upMsgIndex: 0, // 修改的下标
    selectFormData: {
      MF001: '', // 用户编号
      MF002: '', // 用户名称
      MF007: '', // 部门编号
      MF025: 'P'
    }, // 搜索表单
    param: {
        page: 1,
        pageSize: 10,
        total: 0,

    }
  });

  // 选择时间
  const onOk = (dateString, date) => {
    base.userFormData.MODI_DATE = dateString;
  };

  // 初始化userFormData
  const initUserFormData = {
    MF001: '',
    MF002: '',
    MF007: '',
    MODI_DATE: '',
  };
  const handleClick = () => {
    base.visible = true;
    base.userFormData = initUserFormData;
    base.ModelTitle = '新增用户';
  };

  // 检测方式
  const detectionMethod = {
    0: '免检',
    1: '减量',
    2: '正常',
    3: '加严',
    4: '全检'
  } 

  // 检测结果
  const detectionResult = {
    0: '免检',
    1: '减量', 
    2: '正常', 
    3: '加严', 
    4: '全检'
  } 

  // 获取到货检验单列表
  const getArrivalInspectFun = () => {
    getArrivalInspect(base.param).then((res: any) => {
        // console.log(res)
        if(res.code ==  200){
            base.param.total = res.data.total;
            base.data = res.data.list.map((item: any) => {
            //   item.TC018 = taxTypes[item.TC018];
              item.TA004 = sliceTime(item.TA004).replaceAll(":",'');
              item.TA005 = sliceTime(item.TA005).replaceAll(":",'');
              item.TA011 = detectionResult[item.TA011];
              item.TA012 = detectionMethod[item.TA012];
              return item;
            });
          }
    })
  };
  getArrivalInspectFun();


  // commit
  const handleBeforeOk = (done) => {
    base.handleSubmit.validate().then((res) => {
      if (!res) {
        console.log(base.upMsgIndex);
        if (base.upMsgIndex != null) {
          // 修改用户
          base.upMsgIndex = null;
        } else {
          // 添加用户
          // base.data.push(base.userFormData);
        }
        done();
      } else {
        done(false);
      }
    });
  };

  const handleCancel = () => {
    base.visible = false;
  };

  const upUser = (record, rowIndex) => {
    console.log(record);
    base.visible = true;
    base.ModelTitle = '修改用户信息';
    base.userFormData = record;
    base.upMsgIndex = rowIndex;
  };

  const delUser = (record) => {
    console.log(record);
    base.delVisible = true;
    base.delUserMsg = record;
  };

  // 确实删除页面删除
  const delHandleOk = () => {
    console.log('删除');
    console.log(base.delUserMsg);
    base.delVisible = false;
  };

  // 确实删除页面取消
  const delHandleCancel = () => {
    console.log('取消');
    base.delUserMsg = {};
    base.delVisible = false;
  };

  // 查询
  const selectDataMsg = () => {
    console.log(base.selectFormData);
  };

  // 重置SelectFormData
  const resetSelectFormData = () => {
    base.selectFormData.MF001 = '';
    base.selectFormData.MF002 = '';
    base.selectFormData.MF007 = '';
  };


  // 分页
  const pageSizeChange = (val) => {
    // console.log(val);
    base.param.pageSize = val;
    getArrivalInspectFun();
  }

  // 翻页
  const paginationChange = (val) =>{
    base.param.page = val;
    getArrivalInspectFun();
  }

  return {
    base,
    onOk,
    handleClick,
    handleBeforeOk,
    handleCancel,
    upUser,
    delUser,
    delHandleOk,
    delHandleCancel,
    selectDataMsg,
    resetSelectFormData,
    pageSizeChange,
    paginationChange,
  };
}
